<?php

namespace Admin\Models\stats;

use Admin\Models\BaseModel;
use Phalcon\Mvc\Model\Message;

class XhstatsBrushgroup extends BaseModel
{

    /**
     *
     * @var integer
     * @Primary
     * @Column(type="integer", length=11, nullable=false)
     */
    public $id;

    /**
     *
     * @var string
     * @Column(type="string", length=255, nullable=false)
     */
    public $sGroupName;

    /**
     *
     * @var integer
     * @Column(type="integer", length=11, nullable=false)
     */
    public $iSchoolId;

    /**
     *
     * @var string
     * @Column(type="string", length=255, nullable=false)
     */
    public $sClassId;

    /**
     *
     * @var string
     * @Column(type="string", length=255, nullable=false)
     */
    public $sSubject;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $mStandard;

    /**
     *
     * @var integer
     * @Column(type="integer", length=11, nullable=true)
     */
    public $iTopicExecStandard;

    /**
     *
     * @var integer
     * @Column(type="integer", length=4, nullable=true)
     */
    public $iRightRateStandard;

    /**
     *
     * @var integer
     * @Column(type="integer", length=4, nullable=true)
     */
    public $iTopicExecUpStandard;

    /**
     *
     * @var integer
     * @Column(type="integer", length=4, nullable=true)
     */
    public $iTopicExecDownStandard;

    /**
     *
     * @var integer
     * @Column(type="integer", length=4, nullable=true)
     */
    public $iRightRateUpStandard;

    /**
     *
     * @var integer
     * @Column(type="integer", length=4, nullable=true)
     */
    public $iRightRateDownStandard;

    /**
     *
     * @var integer
     * @Column(type="integer", length=4, nullable=true)
     */
    public $bDelete;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $tCreateDate;

    /**
     *
     * @var integer
     * @Column(type="integer", length=20, nullable=true)
     */
    public $iCreateUserId;

    /**
     *
     * @var string
     * @Column(type="string", nullable=true)
     */
    public $tLastModifyDate;

    /**
     *
     * @var integer
     * @Column(type="integer", length=20, nullable=true)
     */
    public $iLastModifyUserId;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        parent::initialize();
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'XHStats_BrushGroup';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhstatsBrushgroup[]|XhstatsBrushgroup
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhstatsBrushgroup
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     * 验证参数
     *
     * @return bool
     *
     * create by ck 20180320
     * modify by ck 20180417
     */
    public function validation()
    {
        $requiredArr = [
            'sGroupName'                => '组名',
            'iSchoolId'                 => '学校id',
            'sClassId'                  => '班级id',
            'sSubject'                  => '科目',
//            'iTopicExecStandard'        => '做题标准',
//            'iRightRateStandard'        => '正确标准',
//            'iTopicExecUpStandard'      => '高题量标准',
//            'iTopicExecDownStandard'    => '低题量标准',
//            'iRightRateUpStandard'      => '高正确标准',
//            'iRightRateDownStandard'    => '低正确标准',
        ];
        foreach ($requiredArr as $key => $name) {
            if (!$this->$key) {
                $this->appendMessage(new Message($name . '缺失', $key, 'InvalidValue'));
                return false;
            }
        }
        if (!is_array($this->mStandard)) {
            $this->appendMessage(new Message('做题标准类型不正确', 'mStandard', 'InvalidValue'));
            return false;
        }
        return true;
    }

    public function afterFetch()
    {
        $this->mStandard = unserialize($this->mStandard);
        return true;
    }

    public function beforeSave()
    {
        $this->mStandard = serialize($this->mStandard);
        return true;
    }

    /**
     * 标准转化为前端可赋值数组
     *
     * @param $standard
     * @param string $prefix
     * @return array
     *
     * create by ck 20180418
     */
    public static function standardToString($standard, $prefix = 'mStandard')
    {
        $strArr = $strArr2 = [];
        foreach ($standard as $key => $value) {
            $strArr["{$prefix}\[{$key}\]"] = $value;
        }
        foreach ($strArr as $key => $value) {
            if (is_array($value)) {
                $strArr2 = array_merge($strArr2, self::standardToString($value, $key));
                unset($strArr[$key]);
            }
        }
        return array_merge($strArr, $strArr2);
    }

}
